package com.zy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zy.bean.bo.chart.ChartRecordsBo;
import com.zy.bean.po.ChartRecords;
import com.zy.bean.qo.ChartSearchQo;
import com.zy.bean.vo.FlowChartVo;
import com.zy.config.SpringContextHolder;
import com.zy.enums.ActveEnum;
import com.zy.mapper.ChartRecordsMapper;
import com.zy.records.VersionRecord;
import com.zy.service.ChartRecordsService;
import com.zy.util.SnowflakeIdWorker;
import com.zy.util.VersionUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

import static org.fatewa.engine.genius.utils.GenericUtils.transaction;

/**
 * @author hanhuafeng
 * @createDate 2023-07-19 12:38:39
 * @description 流程图记录表
 */
@Service
@RequiredArgsConstructor

public class ChartRecordsServiceImpl extends ServiceImpl<ChartRecordsMapper, ChartRecords>
        implements ChartRecordsService {
    private final VersionUtil versionUtil;

    /**
     * 根据项目code获取流程图列表
     *
     * @param projectCode 项目code
     * @return List<FlowChartVo> 流程图列表
     */
    @Override
    public List<FlowChartVo> getChartRecordsByProjectCode(String projectCode) {
        List<FlowChartVo> result = new LinkedList<>();
        List<ChartRecords> chartRecordsList = lambdaQuery().eq(ChartRecords::getProjectCode, projectCode).list();
        if (ObjectUtils.isEmpty(chartRecordsList)) {
            return result;
        }
        chartRecordsList.forEach(chartRecords -> {
            result.add(ChartRecords.tranceBoToVo(chartRecords, versionUtil));
        });
        return result;
    }

    @Override
    public void deleteChartRecordsByProjectCode(String projectCode) {
        LambdaQueryWrapper<ChartRecords> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ChartRecords::getProjectCode, projectCode);
        this.remove(lambdaQueryWrapper);
    }

    /**
     * 创建流程图记录
     *
     * @param chartRecordsBo 流程图记录
     * @return ChartRecords 流程图记录
     */
    @Override
    public ChartRecords createChartRecords(ChartRecordsBo chartRecordsBo) {
        String chartCode = SnowflakeIdWorker.getUuid();
        ChartRecords chartRecords = new ChartRecords();
        transaction((manager, status) -> {
            String versionName = chartRecordsBo.getVersionName();
            if (ObjectUtils.isEmpty(versionName)) {
                versionName = VersionRecord.INIT_VERSION_ENUM.versionName();
            }
            String describe = chartRecordsBo.getDescribe();
            if (ObjectUtils.isEmpty(describe)) {
                describe = VersionRecord.INIT_VERSION_ENUM.desc();
            }
            String versionCode = versionUtil.createVersion(versionName, describe, chartCode, null);
            chartRecords.setCode(chartCode);
            chartRecords.setName(chartRecordsBo.getName());
            chartRecords.setDescribe(chartRecordsBo.getDescribe());
            if (ObjectUtils.isNotEmpty(chartRecordsBo.getIsActive())) {
                chartRecords.setIsActive(chartRecordsBo.getIsActive());
            } else {
                chartRecords.setIsActive(ActveEnum.NOT_ACTIVE.getValue());
            }
            chartRecords.setProjectCode(chartRecordsBo.getProjectCode());
            chartRecords.setActiveVersionCode(versionCode);
            this.save(chartRecords);
        }, SpringContextHolder.getApplicationContext());
        return chartRecords;
    }

    /**
     * 根据项目code分页获取流程图列表
     *
     * @param projectCode 项目code
     * @param qo          查询条件
     * @return Page<FlowChartVo> 流程图列表
     */
    @Override
    public Page<FlowChartVo> getChartRecordsByPage(String projectCode, ChartSearchQo qo) {
        Page<FlowChartVo> result = new Page<>(qo.getCurrent(), qo.getSize());
        LambdaQueryWrapper<ChartRecords> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ChartRecords::getProjectCode, projectCode);
        lambdaQueryWrapper.like(ObjectUtils.isNotEmpty(qo.getChartName()), ChartRecords::getName, qo.getChartName());
        lambdaQueryWrapper.like(ObjectUtils.isNotEmpty(qo.getChartCode()), ChartRecords::getCode, qo.getChartCode());
        lambdaQueryWrapper.orderByDesc(ChartRecords::getId);
        Page<ChartRecords> page = this.page(qo.getPage(), lambdaQueryWrapper);
        List<FlowChartVo> flowChartVoList = new LinkedList<>();
        page.getRecords().forEach(chartRecords -> {
            flowChartVoList.add(ChartRecords.tranceBoToVo(chartRecords, versionUtil));
        });
        result.setRecords(flowChartVoList);
        result.setTotal(page.getTotal());
        return result;
    }

    /**
     * 更新流程图激活状态
     *
     * @param chartCode    流程图code
     * @param activeStatus 激活状态
     */
    @Override
    public void updateChartRecordsActive(String chartCode, Integer activeStatus) {
        ChartRecords chartRecords = this.lambdaQuery().eq(ChartRecords::getCode, chartCode).one();
        if (ObjectUtils.isEmpty(chartRecords)) {
            throw new IllegalArgumentException("流程图不存在");
        }
        if (ObjectUtils.isEmpty(activeStatus)) {
            throw new IllegalArgumentException("激活状态不能为空");
        }
        if (chartRecords.getIsActive().equals(activeStatus)) {
            throw new IllegalArgumentException("激活状态不能重复");
        }
        chartRecords.setIsActive(activeStatus);
        this.updateById(chartRecords);
    }

    /**
     * 更新流程图激活版本号
     *
     * @param chartCode   流程图code
     * @param versionCode 版本号
     */
    @Override
    public void updateChartRecordsActiveVersion(String chartCode, String versionCode) {
        ChartRecords chartRecords = this.lambdaQuery().eq(ChartRecords::getCode, chartCode).one();
        if (ObjectUtils.isEmpty(chartRecords)) {
            throw new IllegalArgumentException("流程图不存在");
        }
        if (ObjectUtils.isEmpty(versionCode)) {
            throw new IllegalArgumentException("版本号不能为空");
        }
        if (chartRecords.getActiveVersionCode().equals(versionCode)) {
            throw new IllegalArgumentException("版本号不能重复");
        }
        chartRecords.setActiveVersionCode(versionCode);
        this.updateById(chartRecords);
    }

    /**
     * 更新流程图记录
     *
     * @param chartCode      流程图code
     * @param chartRecordsBo 流程图记录
     */
    @Override
    public void updateChartRecords(String chartCode, ChartRecordsBo chartRecordsBo) {
        ChartRecords chartRecords = this.lambdaQuery().eq(ChartRecords::getCode, chartCode).one();
        if (ObjectUtils.isEmpty(chartRecords)) {
            throw new IllegalArgumentException("流程图不存在");
        }
        if (ObjectUtils.isNotEmpty(chartRecordsBo.getName())) {
            chartRecords.setName(chartRecordsBo.getName());
        }
        if (ObjectUtils.isNotEmpty(chartRecordsBo.getDescribe())) {
            chartRecords.setDescribe(chartRecordsBo.getDescribe());
        }
        this.updateById(chartRecords);
    }

    /**
     * 删除流程图记录
     *
     * @param chartCode 流程图code
     */
    @Override
    public void deleteChartRecords(String chartCode) {
        LambdaQueryWrapper<ChartRecords> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ChartRecords::getCode, chartCode);
        this.remove(lambdaQueryWrapper);
    }

    /**
     * 根据code获取流程图记录
     *
     * @param chartCode 流程图code
     * @return ChartRecords 流程图记录
     */
    @Override
    public ChartRecords getChartRecordsByCode(String chartCode) {
        LambdaQueryWrapper<ChartRecords> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ChartRecords::getCode, chartCode);
        ChartRecords chartRecords = this.getOne(lambdaQueryWrapper);
        if (ObjectUtils.isNotEmpty(chartRecords)) {
            return chartRecords;
        }
        return null;
    }
}




